<?php
namespace Chat\Controller;
use Chat\Controller\ChatController;

/**
 * 特殊群
 */
class SpecialChatController extends ChatController {
	
	protected $_chat_fee;
	protected $_chat_contribut;
	protected $_chat_contribut_detail;
	
	protected function _initialize(){
		parent::_initialize();
		
		$this->_chat_fee = new \Chat\Model\ChatFeeTurnoverModel();
		$this->_chat_contribut = new \Chat\Model\ChatContributModel();
		$this->_chat_contribut_detail = new \Chat\Model\ChatContributDetailModel();
	}

	
    /**
     * 建经费群
     */
    public function createFundsChatAction(){
    	$chatname = I('request.chatname');
    	$custids = I('request.custids');
    	$amount = I('request.amount');
    	$this->_createFundsChat($chatname , $custids , $amount);
    }
    
    private function _createFundsChat($chatname , $custids , $amount){
    	if(empty($custids) || empty($chatname)){
    		$this->status = 1000;
    		$this->msg = L('STATUS_1000');
    		$this->outPut();
    	}
    	
    	if(bccomp($amount, '0.00', 2) < 0){
    		$this->status = 1000;
    		$this->msg = L('STATUS_1000');
    		$this->outPut();
    	}
    	
    	$custidsArr[] = $this->_custInfo['custid'];
    	$custidsArr2 = explode(",", $custids);
    	$custidsArr = array_merge($custidsArr,$custidsArr2);
    	$custidsArr = array_unique($custidsArr);
    	 
    	$data = array();
    	$data['chatname'] = $chatname;
    	$data['grouptype'] = 1;
    	$data['chattype'] = 3;
    	$data['custid'] = $this->_custInfo['custid'];
    	$data['maxcustnum'] = 1000000;
    	$data['status'] = C('DEFAULT_CHAT_STATUS');
    	$data['amount'] = (float)$amount;
    	$chatid = $this->_chat->addChat($data);
    	if($chatid){
    		$arr = array();
    		$arr['chatid'] = $chatid;
    		$arr['custid'] = $this->_custInfo['custid'];
    		$arr['status'] = 1;
    		$arr['nickname'] = $this->_custInfo['nickname'];
    		$ret = $this->_chat_cust->addCust($arr);
    		if($ret){
    			$this->_delChatListCache($this->_custInfo['custid']);
    			\im::addGroup($chatid, array($this->_custInfo['custid']));
    			 
    			$this->_chat->custNumInr($chatid , 1);
    		}
    		
    		$arr = array();
    		$arr['chatid'] = $chatid;
    		$arr['custid'] = $this->_custInfo['custid'];
    		$arr['amount'] = $data['amount'];
    		$arr['content'] = "入群经费";
    		$arr['type'] = 1;
    		$contributid = $this->_chat_contribut->addContribut($arr);
    		if(!empty($contributid) && !empty($ret)){
	    		foreach($custidsArr as $custid){
	    			$custInfo = $this->_getCustInfo($custid);//用户不存在，就跳过
	    			if(empty($custInfo))continue;
	    			
					if($custid == $this->_custInfo['custid']){
						$this->_addChatFeeTurnover($chatid,$this->_custInfo['custid'],$data['amount'],1,'群成员入群费用' , false);
							
						$arr = array();
						$arr['contributid'] = $contributid;
						$arr['chatid'] = $chatid;
						$arr['custid'] = $this->_custInfo['custid'];
						$arr['amount'] = $data['amount'];
						$arr['status'] = 1;
						$this->_chat_contribut_detail->addContributDetail($arr);
							
						//缴费累加
						$this->_chat_contribut->addTotalamount($contributid , $data['amount']);
						
						if(bccomp($data['amount'], '0.00', 2) > 0){
							$msg = array();
							$msg['from_cust_name'] = $this->_custInfo['nickname'];
							$msg['from_cust_id'] = (int)$this->_custInfo['custid'];
							$msg['chatid'] = (int)$chatid;
							$msg['type'] = 10003;
							$msg['session_id'] = (int)$chatid;
							$msg['session_type'] = 2;
							//$msg['content'] = "{$this->_custInfo['nickname']}缴纳{$data['amount']}元经费";
							$msg['content'] = "{0}缴纳{$data['amount']}元经费";
							$msg['content_param'][0] = array('custid'=>$this->_custInfo['custid'],'nickname'=>$this->_custInfo['nickname']);
							
							$msg['amount'] = $data['amount'];
							$msg['operationType'] = 3;
							\im::sendMsg(2, array(), array($chatid), $msg);
						}
					}else{
						$result = $this->_chat_cust->getCust($chatid,$custid);//如果已经是群成员，就跳过
						if(!empty($result))continue;
						
						//$content = L('INVITE_JOIN_CHAT_NOTICE',array('nickname' => $this->_custInfo['nickname'],'tnickname' => $custInfo['nickname']));//xxx邀请xxx加入群聊
						$content = "{0}邀请{1}加入经费群";
						$content_param[0] = array('custid'=>$this->_custInfo['custid'],'nickname'=>$this->_custInfo['nickname']);
						$content_param[1] = array('custid'=>$custid,'nickname'=>$custInfo['nickname']);
						 
						$this->_sendInviteNoticeMsg($contributid , $chatid , $custid , $this->_custInfo['custid'] , $this->_custInfo , $data['amount'] , $content , $content_param);
						$this->_sendInviteNoticeMsg($contributid , $chatid , $this->_custInfo['custid'] , $custid , $this->_custInfo , $data['amount'] , $content , $content_param);
					}
				}
    		}
    		
    		
    		$chatInfo = $this->_getChatInfoById($chatid);
    		
    		$this->data = $chatInfo;
    		$this->outPut();
    	}else{
    		$this->status = 1401;
    		$this->msg = L('STATUS_1401');
    		$this->outPut();
    	}
    }
    
    
    private function _sendInviteNoticeMsg($contributid , $chatid, $tcustid , $sessioncustid , $custInfo , $amount , $content , $content_param) {
    	$msg['from_cust_name'] = $custInfo['nickname'];
    	$msg['from_cust_id'] = (int)$custInfo['custid'];
    	$msg['to_cust_id'] = (int)$tcustid;
    	$msg['contributid'] = (int)$contributid;
    	$msg['chatid'] = (int)$chatid;
    	$msg['type'] = 23;
    	$msg['session_id'] = (int)$sessioncustid;
    	$msg['session_type'] = 1;
    	$msg['amount'] = $amount;
    	$msg['content'] = $content;
    	$msg['content_param'] = $content_param;
    	\im::sendMsg(3, array($tcustid), array(), $msg);
    }
    
    /**
     * 邀请增加聊天群组人员
     */
    public function inviteChatCustsAction(){
    	$chatid = (int)I('request.chatid');
    	$custids = I('request.custids');
    	$this->_inviteChatCusts($chatid,$custids);
    }
    
    
    
    private function _inviteChatCusts($chatid,$custids){
    	if(empty($chatid) || empty($custids)){
    		$this->status = 1000;
    		$this->msg = L('STATUS_1000');
    		$this->outPut();
    	}
    
    	$chatInfo = $this->_checkCurrentCust($chatid);
    	
    	
    	$custidsArr = explode(",", $custids);
    	$custidsArr[] = $this->_custInfo['custid'];
    	$custidsArr = array_unique($custidsArr);
    	
    	$arr = array();
    	$arr['chatid'] = $chatid;
    	$arr['custid'] = $chatInfo['custid'];
    	$arr['amount'] = $chatInfo['amount'];
    	$arr['content'] = "入群经费";
    	$arr['type'] = 1;
    	$contributid = $this->_chat_contribut->addContribut($arr);
    	if(!empty($contributid)){
    		$custNum = 0;
    		foreach($custidsArr as $custid){
    			if($custNum > (int)$chatInfo['maxcustnum']-(int)$chatInfo['custnum'])break;//超出最大数量，就跳过
    			
    			$custInfo = $this->_getCustInfo($custid);//用户不存在，就跳过
    			if(empty($custInfo))continue;
    			 
    			$result = $this->_chat_cust->getCust($chatid,$custid);//如果已经是群成员，就跳过
    			if(!empty($result))continue;
    	
				//$content = L('INVITE_JOIN_CHAT_NOTICE',array('nickname' => $this->_custInfo['nickname'],'tnickname' => $custInfo['nickname']));//xxx邀请xxx加入群聊
				$content = "{0}邀请{1}加入经费群";
				$content_param[0] = array('custid'=>$this->_custInfo['custid'],'nickname'=>$this->_custInfo['nickname']);
				$content_param[1] = array('custid'=>$custid,'nickname'=>$custInfo['nickname']);
				
				$this->_sendInviteNoticeMsg($contributid , $chatid , $custid , $this->_custInfo['custid'] , $this->_custInfo , $chatInfo['amount'] , $content , $content_param);
				$this->_sendInviteNoticeMsg($contributid , $chatid , $this->_custInfo['custid'] , $custid , $this->_custInfo , $chatInfo['amount'] , $content , $content_param);
				
				$custNum++;
    		}

    		$this->outPut();
    	}else{
    		$this->status = 1438;
    		$this->msg = L('STATUS_1438');
    		$this->outPut();
    	}
    }
    
    
    /**
     * 接受某人邀请加入聊天群组
     */
    public function acceptInviteAction(){
    	$contributid = (int)I('request.contributid');
    	$invitecustid = (int)I('request.invitecustid');
    	$accept = (int)I('request.accept');
    	$paypassword  = I('request.paypassword');
    	$this->_acceptInvite($contributid , $invitecustid  , $accept , $paypassword);
    }
    
    
    private function _acceptInvite($contributid , $invitecustid , $accept = 1 , $paypassword = ""){
    	if(empty($contributid) || empty($invitecustid)){
    		$this->status = 1000;
    		$this->msg = L('STATUS_1000');
    		$this->outPut();
    	}
    	
    	$invitecustInfo = $this->_getCustInfo($invitecustid);
    	if(empty($invitecustInfo)){
    		$this->status = 1001;
    		$this->msg = L('STATUS_1001');
    		$this->outPut();
    	}
    	 
    	//check contribut
    	$contributInfo = $this->_chat_contribut->getContributById($contributid);
    	if(!empty($contributInfo)){
    		$chatid = $contributInfo['chatid'];
    		$chatInfo = $this->_chat->getChatInfoById($chatid);
    		if(empty($chatInfo)){
    			$this->status = 1001;
    			$this->msg = L('STATUS_1001');
    			$this->outPut();
    		}
    		
    		$ret = $this->_chat_cust->getCust($chatid, $this->_custInfo['custid']);
    		if(!empty($ret)){
    			$this->outPut();
    		}
    	}else{
    		$this->status = 1001;
    		$this->msg = L('STATUS_1001');
    		$this->outPut();
    	}
    	 
    	if(empty($accept)){//拒绝
    		$this->outPut();
    	}
    	
    	
    	//付款(入群费用)
    	$purse = new \Purse\Controller\PurseController();
    	$ret = $purse->pay($contributInfo['custid'] , $chatInfo['amount'] , 0 , $paypassword ,array('群费用支出','群费用收入') , false);
    	if(!empty($ret)){
    		//经费群资金记录
    		$this->_addChatFeeTurnover($contributInfo['chatid'],$this->_custInfo['custid'],$chatInfo['amount'],1,'群成员入群费用',false);
    		
    		//缴费累加
    		$this->_chat_contribut->addTotalamount($contributid , $chatInfo['amount']);
    		
    		$arr = array();
    		$arr['contributid'] = $contributid;
    		$arr['chatid'] = $chatid;
    		$arr['custid'] = $this->_custInfo['custid'];
    		$arr['amount'] = $chatInfo['amount'];
    		$arr['status'] = 1;
    		$this->_chat_contribut_detail->addContributDetail($arr);
	    		
	    	if(bccomp($chatInfo['amount'], '0.00', 2) > 0){	
	    		$msg = array();
	    		$msg['from_cust_name'] = $this->_custInfo['nickname'];
	    		$msg['from_cust_id'] = (int)$this->_custInfo['custid'];
	    		$msg['chatid'] = (int)$contributInfo['chatid'];
	    		$msg['type'] = 10003;
	    		$msg['session_id'] = (int)$contributInfo['chatid'];
	    		$msg['session_type'] = 2;
	    		//$msg['content'] = "{$this->_custInfo['nickname']}缴纳{$chatInfo['amount']}元经费";
	    		$msg['content'] = "{0}缴纳{$chatInfo['amount']}元经费";
	    		$msg['content_param'][0] = array('custid'=>$this->_custInfo['custid'],'nickname'=>$this->_custInfo['nickname']);
	    		
	    		$msg['amount'] = $chatInfo['amount'];
	    		$msg['operationType'] = 3;
	    		\im::sendMsg(2, array(), array($contributInfo['chatid']), $msg);
    		}
    		
    		$data = array();
    		$data['chatid'] = $chatid;
    		$data['custid'] = $this->_custInfo['custid'];
    		$data['status'] = 1;
    		$ret = $this->_chat_cust->addCust($data);
    		if($ret){
    			$this->_chat->custNumInr($chatid , 1);
    			$this->_delChatCustCache($chatid);
    			$this->_delChatListCache($this->_custInfo['custid']);
    		
    			//$content = L('ACCEPT_INVITE_CHAT_NOTICE',array('nickname' => $this->_custInfo['nickname']));//xxx接受邀请加入该群
    			$content = "{0}邀请{1}加入了群聊";
    			$content_param[0] = array('custid'=>$invitecustid,'nickname'=>$invitecustInfo['nickname']);
    			$content_param[1] = array('custid'=>$this->_custInfo['custid'],'nickname'=>$this->_custInfo['nickname']);
    		
    			//发推送消息
    			$data = array();
    			$data[0]['custid'] = $this->_custInfo['custid'];
    			$data[0]['nickname'] = $this->_custInfo['nickname'];
    			$data[0]['chatid'] = $chatid;
    			$this->_addCustsNotice($chatid , $chatInfo , array($this->_custInfo['custid']) , $content , $content_param , $data);
    		
    			$this->_setAvatarNotice($chatid);
    		
    			$this->outPut();
    		}else{
    			$this->status = 1437;
    			$this->msg = L('STATUS_1437');
    			$this->outPut();
    		}
    	}else{
    		//付款未成功
    		$this->status = 2606;
    		$this->msg = L('STATUS_2606');
    		$this->outPut();
    	}
    }
    
    
    
    
    

    /**
     * 扫描二维码加入聊天群组
     */
    public function joinChatAction(){
    	$chatid = (int)I('request.chatid');
    	$paypassword  = I('request.paypassword');
    	$this->_joinChat($chatid , $paypassword);
    }
    
    
    private function _joinChat($chatid , $paypassword = ""){
    	if(empty($chatid)){
    		$this->status = 1000;
    		$this->msg = L('STATUS_1000');
    		$this->outPut();
    	}
    
    	//check chat
        $chatInfo = $this->_chat->getChatInfoById($chatid);
    	if(empty($chatInfo)){
    		$this->status = 1001;
    		$this->msg = L('STATUS_1001');
    		$this->outPut();
    	}
    	
    	
    	$contributInfo = array();
    	$contributInfo['chatid'] = $chatid;
    	$contributInfo['custid'] = $chatInfo['custid'];
    	$contributInfo['amount'] = $chatInfo['amount'];
    	$contributInfo['content'] = "入群经费";
    	$contributInfo['type'] = 1;
    	 
        //付款(入群费用)
    	$purse = new \Purse\Controller\PurseController();
    	$ret = $purse->pay($contributInfo['custid'] , $chatInfo['amount'] , 0 , $paypassword ,array('群费用支出','群费用收入') , false);
    	if(!empty($ret)){
    		$contributid = $this->_chat_contribut->addContribut($contributInfo);
    		
    		//经费群资金记录
    		$this->_addChatFeeTurnover($contributInfo['chatid'],$this->_custInfo['custid'],$chatInfo['amount'],1,'群成员入群费用',false);
    		
    		//缴费累加
    		$this->_chat_contribut->addTotalamount($contributid , $chatInfo['amount']);
    		
    		$arr = array();
    		$arr['contributid'] = $contributid;
    		$arr['chatid'] = $chatid;
    		$arr['custid'] = $this->_custInfo['custid'];
    		$arr['amount'] = $chatInfo['amount'];
    		$arr['status'] = 1;
    		$this->_chat_contribut_detail->addContributDetail($arr);
	    		
	    	if(bccomp($chatInfo['amount'], '0.00', 2) > 0){	
	    		$msg = array();
	    		$msg['from_cust_name'] = $this->_custInfo['nickname'];
	    		$msg['from_cust_id'] = (int)$this->_custInfo['custid'];
	    		$msg['chatid'] = (int)$chatid;
	    		$msg['type'] = 10003;
	    		$msg['session_id'] = (int)$chatid;
	    		$msg['session_type'] = 2;
	    		//$msg['content'] = "{$this->_custInfo['nickname']}缴纳{$chatInfo['amount']}元经费";
	    		$msg['content'] = "{0}缴纳{$chatInfo['amount']}元经费";
	    		$msg['content_param'][0] = array('custid'=>$this->_custInfo['custid'],'nickname'=>$this->_custInfo['nickname']);
	    		
	    		$msg['amount'] = $chatInfo['amount'];
	    		$msg['operationType'] = 3;
	    		\im::sendMsg(2, array(), array($chatid), $msg);
    		}
    		
    		
    		$data = array();
    		$data['chatid'] = $chatid;
    		$data['custid'] = $this->_custInfo['custid'];
    		$data['status'] = 1;
    		$ret = $this->_chat_cust->addCust($data);
    		if($ret){
    			$this->_chat->custNumInr($chatid , 1);
    			$this->_delChatCustCache($chatid);
    			$this->_delChatListCache($this->_custInfo['custid']);
    		
    			//$content = "{$this->_custInfo['nickname']}加入了该群";//L('ACCEPT_INVITE_CHAT_NOTICE',array('nickname' => $this->_custInfo['nickname']));//xxx接受邀请加入该群
    			//$content_param = [];
    			$content = "{0}邀请{1}加入经费群";
    			$custInfo = $this->_getCustInfo($chatInfo['custid']);
    			$content_param[0] = array('custid'=>$chatInfo['custid'],'nickname'=>$custInfo['nickname']);
    			$content_param[1] = array('custid'=>$this->_custInfo['custid'],'nickname'=>$this->_custInfo['nickname']);
    		
    			//发推送消息
    			$data = array();
    			$data[0]['custid'] = $this->_custInfo['custid'];
    			$data[0]['nickname'] = $this->_custInfo['nickname'];
    			$data[0]['chatid'] = $chatid;
    			$this->_addCustsNotice($chatid , $chatInfo , array($this->_custInfo['custid']) , $content , $content_param , $data);
    		
    			$this->_setAvatarNotice($chatid);
    		
    			$this->outPut();
    		}else{
    			$this->status = 1437;
    			$this->msg = L('STATUS_1437');
    			$this->outPut();
    		}
    	}else{
    	    //付款未成功
    		$this->status = 2606;
    		$this->msg = L('STATUS_2606');
    		$this->outPut();
    	}
    }
    
    
    
    
    
    
    /**
     * 记录群经费收支
     */
    public function addChatFeeTurnoverAction(){
    	$chatid = (int)I('request.chatid');
    	$custid = (int)I('request.custid');
    	$amount = I('request.amount');
    	$direction = (int)I('request.direction');
    	$content = I('request.content');
    	$this->_addChatFeeTurnover($chatid,$custid,$amount,$direction,$content);
    }
    
    private function _addChatFeeTurnover($chatid,$custid,$amount,$direction,$content,$isReturn=true){
    	if(empty($chatid) || !in_array($direction , [1,2])){
    		$this->status = 1000;
    		$this->msg = L('STATUS_1000');
    		$this->outPut();
    	}
    	
    	if(bccomp($amount, '0.00', 2) == 0){
    		if($isReturn){
    			$this->outPut();
    		}else{
    			return true;
    		}
    	}
    	
    	
    	//经费群资金记录
    	$data = array();
    	$data['chatid'] = $chatid;
    	$data['custid'] = $custid;
    	$data['amount'] = $amount;
    	$data['direction'] = $direction;
    	$data['content'] = $content;
    	$ret = $this->_chat_fee->addTurnover($data);
    	if($ret){
    		if($isReturn){
    			$msg = array();
    			$msg['from_cust_name'] = $this->_custInfo['nickname'];
    			$msg['from_cust_id'] = (int)$this->_custInfo['custid'];
    			$msg['chatid'] = (int)$chatid;
    			$msg['type'] = 10001;
    			$msg['session_id'] = (int)$chatid;
    			$msg['session_type'] = 2;
    			$msg['amount'] = $amount;
    			//$msg['content'] = "{$this->_custInfo['nickname']}记了{$amount}元经费支出";
    			$msg['content'] = "{0}记了{$amount}元经费支出";
    			$msg['content_param'][0] = array('custid'=>$this->_custInfo['custid'],'nickname'=>$this->_custInfo['nickname']);
    			
    			$msg['operationType'] = 3;
    			\im::sendMsg(2, array(), array($chatid), $msg);
    			
    			$this->outPut();
    		}else{
    			return true;
    		}
    	}
    }
    
    
    /**
     * 撤销群经费收支记录
     */
    public function delChatFeeTurnoverAction(){
    	$chatid = (int)I('request.chatid');
    	$id = (int)I('request.id');
    	$this->_delChatFeeTurnover($id,$chatid);
    }
    
    private function _delChatFeeTurnover($id,$chatid){
    	if(empty($id) || empty($chatid)){
    		$this->status = 1000;
    		$this->msg = L('STATUS_1000');
    		$this->outPut();
    	}
    	 
    	$this->_checkCurrentCust($chatid , 1);
    	
    	$turnoverInfo = $this->_chat_fee->getInfoById($id);
    	if(empty($turnoverInfo)){
    		$this->status = 1001;
    		$this->msg = L('STATUS_1001');
    		$this->outPut();
    	}
    	 
    	//撤销经费群资金记录
    	$data = array();
    	$data['id'] = $id;
    	$data['chatid'] = $chatid;
    	$ret = $this->_chat_fee->delTurnover($data);
    	if($ret){
    		$msg = array();
    		$msg['from_cust_name'] = $this->_custInfo['nickname'];
    		$msg['from_cust_id'] = (int)$this->_custInfo['custid'];
    		$msg['chatid'] = (int)$chatid;
    		$msg['type'] = 10001;
    		$msg['session_id'] = (int)$chatid;
    		$msg['session_type'] = 2;
    		$msg['amount'] = $turnoverInfo['amount'];
    		//$msg['content'] = "{$this->_custInfo['nickname']}删除了一笔{$turnoverInfo['amount']}元的经费支出";  
    		$msg['content'] = "{0}删除了一笔{$turnoverInfo['amount']}元的经费支出";
    		$msg['content_param'][0] = array('custid'=>$this->_custInfo['custid'],'nickname'=>$this->_custInfo['nickname']);
    		
    		$msg['operationType'] = 3;
    		\im::sendMsg(2, array(), array($chatid), $msg);
    	}
    	
    	$this->outPut();
    }
    
    
    /**
     * 群经费支出记录列表
     */
    public function chatFeeTurnoverListAction(){
    	$chatid = (int)I('request.chatid');
    	$direction = (int)I('request.direction');
    	$this->_chatFeeTurnoverList($chatid,$direction);
    }
    
    private function _chatFeeTurnoverList($chatid,$direction){
    	if(empty($chatid) || empty($direction)){
    		$this->status = 1000;
    		$this->msg = L('STATUS_1000');
    		$this->outPut();
    	}
    	
    	$this->_checkCurrentCust($chatid);
    	 
    	//经费群资金记录
    	$data = array();
    	$data['chatid'] = $chatid;
    	$data['direction'] = 2;//$direction;
    	$ret = $this->_chat_fee->getTurnoverList($data);
    	if(!empty($ret['data'])){
    		$this->data = $ret['data'];
    	}
    	
    	$this->outPut();
    }
    
    
    /**
     * 群经费收入记录列表
     */
    public function revenueListAction(){
    	$chatid = (int)I('request.chatid');
    	$this->_revenueList($chatid);
    }
    
    private function _revenueList($chatid){
    	if(empty($chatid)){
    		$this->status = 1000;
    		$this->msg = L('STATUS_1000');
    		$this->outPut();
    	}
    	
    	$this->_checkCurrentCust($chatid);
    	
    	$data['chatid'] = $chatid;
    	$contributList = $this->_chat_contribut->getContributList($data);
    	if(!empty($contributList['data'])){
    		$contributids = array();
    		foreach($contributList['data'] as &$contribut){
    			$contribut['detailList'] = $this->_chat_contribut_detail->getDetailList(array('chatid'=>$chatid , 'contributid'=>$contribut['contributid']));
    			if(!empty($contribut['detailList'])){
    				foreach($contribut['detailList'] as &$item){
    					$custInfo = $this->_getCustInfo($item['custid']);
    					$item['nickname'] = $custInfo['nickname'];
    				}
    			}
    		}
    	}
    	
    	$this->data = $contributList['data'];
    	$this->outPut();
    }
    
    
    
    
    
    /**
     * 设置入群经费
     */
    public function setChatAmountAction(){
    	$chatid = (int)I('request.chatid');
    	$amount = I('request.amount');
    	$this->_setChatAmount($chatid , $amount);
    }
    
    
    private function _setChatAmount($chatid , $amount){
    	if(empty($chatid)){
    		$this->status = 1000;
    		$this->msg = L('STATUS_1000');
    		$this->outPut();
    	}
    	
    	if(bccomp($amount, '0.00', 2) < 0){
    		$this->status = 1000;
    		$this->msg = L('STATUS_1000');
    		$this->outPut();
    	}
    	 
    	$this->_checkCurrentCust($chatid , 1);
    	$data = array();
    	$data['chatid'] = $chatid;
    	$data['amount'] = number_format($amount, 2, '.', '');
    	$ret = $this->_chat->updateChat($data);
    	if($ret){
    		$arr = array();
    		$arr['amount'] = $data['amount'];
    		$arr['chatid'] = $data['chatid'];
    		$this->_chat_contribut->changeAmount($arr);
    		
    		$msg = array();
    		$msg['from_cust_name'] = $this->_custInfo['nickname'];
    		$msg['from_cust_id'] = (int)$this->_custInfo['custid'];
    		$msg['chatid'] = (int)$chatid;
    		$msg['type'] = 10004;
    		$msg['session_id'] = (int)$chatid;
    		$msg['session_type'] = 2;
    		//$msg['content'] = "{$this->_custInfo['nickname']}将入群条件更改为{$data['amount']}元";
    		$msg['content'] = "{0}将入群条件更改为{$data['amount']}元";
    		$msg['content_param'][0] = array('custid'=>$this->_custInfo['custid'],'nickname'=>$this->_custInfo['nickname']);
    		
    		$msg['amount'] = $data['amount'];
    		$msg['operationType'] = 3;
    		\im::sendMsg(2, array(), array($chatid), $msg);
    		
    		$this->outPut();
    	}else{
    		$this->status = 1416;
    		$this->msg = L('STATUS_1416');
    		$this->outPut();
    	}
    }
    
    
    
    
    /**
     * 催缴费用
     */
    public function contributAction(){
    	$chatid = (int)I('request.chatid');
    	$amount = I('request.amount');
    	$this->_contribut($chatid,$amount);
    }
    
    private function _contribut($chatid,$amount){
    	if(empty($chatid)){
    		$this->status = 1000;
    		$this->msg = L('STATUS_1000');
    		$this->outPut();
    	}
    	
        if(bccomp($amount, '0.01', 2) < 0){
    		$this->status = 1000;
    		$this->msg = L('STATUS_1000');
    		$this->outPut();
    	}
    	
    	$chatInfo = $this->_checkCurrentCust($chatid , 1);
    	
    	$custList = $this->_getChatCust($chatid);
    	if(empty($custList) || count($custList) == 1){
    		$this->status = 1439;
    		$this->msg = L('STATUS_1439');
    		$this->outPut();
    	}
    	
    	
    	$data = array();
    	$data['chatid'] = $chatid;
    	$data['custid'] = $chatInfo['custid'];
    	$data['amount'] = $amount;
    	$data['content'] = "收经费";
    	$data['type'] = 2;
    	$contributid = $this->_chat_contribut->addContribut($data);
    	if(!empty($contributid)){
    		foreach($custList as $cust){
				if($cust['custid'] == $this->_custInfo['custid']){
					$this->_addChatFeeTurnover($chatid,$this->_custInfo['custid'],$amount,1,'群成员缴纳费用' , false);
					
					$arr = array();
					$arr['contributid'] = $contributid;
					$arr['chatid'] = $chatid;
					$arr['custid'] = $this->_custInfo['custid'];
					$arr['amount'] = $amount;
					$arr['status'] = 1;
					$this->_chat_contribut_detail->addContributDetail($arr);
					
					//缴费累加
					$this->_chat_contribut->addTotalamount($contributid , $amount);
				}else{
					$arr = array();
					$arr['contributid'] = $contributid;
					$arr['chatid'] = $chatid;
					$arr['custid'] = $cust['custid'];
					$arr['amount'] = $amount;
					$this->_chat_contribut_detail->addContributDetail($arr);
				}
				
			}
			
			$msg = array();
			$msg['from_cust_name'] = $this->_custInfo['nickname'];
			$msg['from_cust_id'] = (int)$this->_custInfo['custid'];
			$msg['chatid'] = (int)$chatid;
			$msg['type'] = 10002;
			$msg['session_id'] = (int)$chatid;
			$msg['session_type'] = 2;
			$msg['contributid'] = $contributid;
			$msg['content'] = "每人收经费";
			$msg['content_param'] = [];
			$msg['amount'] = $amount;
			$msg['operationType'] = 3;
			\im::sendMsg(2, array(), array($chatid), $msg);
			
			sleep(1);
			
			$msg = array();
			$msg['from_cust_name'] = $this->_custInfo['nickname'];
			$msg['from_cust_id'] = (int)$this->_custInfo['custid'];
			$msg['chatid'] = (int)$chatid;
			$msg['type'] = 10003;
			$msg['session_id'] = (int)$chatid;
			$msg['session_type'] = 2;
			//$msg['content'] = "{$this->_custInfo['nickname']}缴纳{$amount}元经费";
			$msg['content'] = "{0}缴纳{$amount}元经费";
			$msg['content_param'][0] = array('custid'=>$this->_custInfo['custid'],'nickname'=>$this->_custInfo['nickname']);
			
			$msg['amount'] = $amount;
			$msg['operationType'] = 3;
			\im::sendMsg(2, array(), array($chatid), $msg);
			
			
			$this->outPut();
    	}
    }
    
    
    
    /**
     * 转移群主
     */
    public function setChatCreatorAction(){
    	$chatid = (int)I('request.chatid');
    	$custid = (int)I('request.custid');
    	$this->_setChatCreator($chatid , $custid);
    }
    
    
    private function _setChatCreator($chatid , $custid){
    	if(empty($chatid) || empty($custid)){
    		$this->status = 1000;
    		$this->msg = L('STATUS_1000');
    		$this->outPut();
    	}
    	 
    	$this->_checkCurrentCust($chatid , 1);
    	 
    	$result = $this->_chat_cust->getCust($chatid,$custid);
    	if(empty($result)){
    		$this->status = 1433;
    		$this->msg = L('STATUS_1433');
    		$this->outPut();
    	}
    	
    	$data = array();
    	$data['chatid'] = $chatid;
    	$data['custid'] = $custid;
    	$ret1 = $this->_chat_contribut->updateContributByChatid($data);
    	 
    	$data = array();
    	$data['chatid'] = $chatid;
    	$data['custid'] = $custid;
    	$ret2 = $this->_chat->updateChat($data);
    	if($ret1 && $ret2){
    		$custInfo = $this->_getCustInfo($custid);
    		$msg['from_cust_name'] = $this->_custInfo['nickname'];
    		$msg['from_cust_id'] = (int)$this->_custInfo['custid'];
    		$msg['type'] = 24;
    		$msg['session_id'] = (int)$chatid;
    		$msg['session_type'] = 2;
    		$msg['content'] = "{0}已成为新群主";
    		$msg['content_param'][0] = array('custid'=>$custid,'nickname'=>$custInfo['nickname']);
    		$msg['operationType'] = 3;
    		\im::sendMsg(2, array(), array($chatid), $msg);
    
    		$this->outPut();
    	}else{
    		$this->status = 1431;
    		$this->msg = L('STATUS_1431');
    		$this->outPut();
    	}
    }
    
    
    /**
     * 缴费
     */
    public function doContributAction(){
    	$contributid = (int)I('request.contributid');
    	$paypassword  = I('request.paypassword');
    	$this->_doContribut($contributid , $paypassword);
    }
    
    
    private function _doContribut($contributid , $paypassword=""){
    	if(empty($contributid)){
    		$this->status = 1000;
    		$this->msg = L('STATUS_1000');
    		$this->outPut();
    	}
    
    	//check contribut
    	$contributInfo = $this->_chat_contribut->getContributById($contributid);
    	if(empty($contributInfo)){
    		$this->status = 1001;
    		$this->msg = L('STATUS_1001');
    		$this->outPut();
    	}
    	
    	if(bccomp($contributInfo['amount'], '0.01', 2) < 0){
    		$this->outPut();
    	}
    	
    	$contributDetail = $this->_chat_contribut_detail->getContributDetail(array('contributid'=>$contributid,'custid'=>$this->_custInfo['custid']));
    	if(empty($contributDetail) || $contributDetail['status'] == 1){
    		$this->status = 1001;
    		$this->msg = L('STATUS_1001');
    		$this->outPut();
    	}
    	 
    	//付款(入群费用)
    	$purse = new \Purse\Controller\PurseController();
    	$ret = $purse->pay($contributInfo['custid'] , $contributInfo['amount'] , 0 , $paypassword ,array('群费用支出','群费用收入') , false);
    	if(!empty($ret[0]) && !empty($ret[1])){
    		//经费群资金记录
    		$this->_addChatFeeTurnover($contributInfo['chatid'],$this->_custInfo['custid'],$contributInfo['amount'],1,'群成员缴纳费用' , false);
    		
    		//缴费累加
    		$this->_chat_contribut->addTotalamount($contributid , $contributInfo['amount']);
    		
    		$arr = array();
    		$arr['contributid'] = $contributid;
    		$arr['custid'] = $this->_custInfo['custid'];
    		$arr['status'] = 1;
    		$this->_chat_contribut_detail->updateContributDetail($arr);
    		
    		$msg = array();
    		$msg['from_cust_name'] = $this->_custInfo['nickname'];
    		$msg['from_cust_id'] = (int)$this->_custInfo['custid'];
    		$msg['chatid'] = (int)$contributInfo['chatid'];
    		$msg['type'] = 10003;
    		$msg['session_id'] = (int)$contributInfo['chatid'];
    		$msg['session_type'] = 2;
    		//$msg['content'] = "{$this->_custInfo['nickname']}缴纳{$contributInfo['amount']}元经费";
    		$msg['content'] = "{0}缴纳{$contributInfo['amount']}元经费";
    		$msg['content_param'][0] = array('custid'=>$this->_custInfo['custid'],'nickname'=>$this->_custInfo['nickname']);
    		
    		$msg['amount'] = $contributInfo['amount'];
    		$msg['operationType'] = 3;
    		\im::sendMsg(2, array(), array($contributInfo['chatid']), $msg);
    		
    		$this->outPut();
    	}else{
    		//付款未成功
    		$this->status = 2606;
    		$this->msg = L('STATUS_2606');
    		$this->outPut();
    	}
    }
    
    
    /**
     * 获取经费群组的余额信息
     */
    public function getFundsChatAmountAction(){
    	$chatid = (int)I('request.chatid');
    	$this->_getFundsChatAmount($chatid);
    }
    
    private function _getFundsChatAmount($chatid){
    	if(empty($chatid)){
    		$this->status = 1000;
    		$this->msg = L('STATUS_1000');
    		$this->outPut();
    	}
    
    	$chatInfo = $this->_getChatInfoById($chatid);
    	if($chatInfo['custid'] == $this->_custInfo['custid']){//判断当前用户是否是群主
    		$chatInfo['_isadmin'] = "1";
    		$chatInfo['nickname'] = $this->_custInfo['nickname'];
    	}else{
    		$chatInfo['_isadmin'] = "0";
    		$custInfo = $this->_getCustInfo($chatInfo['custid']);
    		$chatInfo['nickname'] = $custInfo['nickname'];
    	}
    	 
    	$chatInfo['leftAmount'] = 0.00;
    	$ret = $this->_chat_fee->getLeftAmount($chatid);
    	if(!empty($ret)){
    		foreach($ret as $fee){
	    		if($fee['direction'] == "1") 
	    			$chatInfo['leftAmount'] = $chatInfo['leftAmount'] + $fee['amount'];
	    		else 
	    			$chatInfo['leftAmount'] = $chatInfo['leftAmount'] - $fee['amount'];
    		}
    	}
    	
    	$this->data = $chatInfo;
    	$this->outPut();
    }

}
